package com.itboye.textileexpres.myset;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPUtils;

public class ActivityPersonSign extends BaseActivity implements
		OnClickListener, Observer, TextWatcher {
	ImageView back;
	ImageView img_other;
	TextView txt_save;
	TextView title;
	MainPresenter mainPresenter;
	EditText edit;
	String nick;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_person_sign;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		img_other.setVisibility(View.GONE);
		txt_save.setVisibility(View.VISIBLE);
		title.setText("签名");
		txt_save.setEnabled(false);
		mainPresenter = new MainPresenter(this);
		edit.addTextChangedListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.txt_save:
			nick = edit.getText() + "";
			if (nick.length() > 30) {
				MyToast.toast("长度不能超过30");
				return;
			}
			UserBean bean = new UserBean();
			bean.setUid(IsMobileNumPhone.isUid());
			bean.setSign(nick);
			mainPresenter.updateUserInfoByUid(bean);

			break;
		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType().equals(
					MainPresenter.UpdateUserInfoSuccessEvent)) {
				toast(entity.getData() + "");
				SPUtils.put(this, null, SpPublicParams.SIGNATURE, nick);
				Intent intent = new Intent("updatechange");
				sendBroadcast(intent);
				finish();
			} else if (entity.getEventType().equals(
					MainPresenter.UpdateUserInfoFailureEvent)) {
				toast(entity.getData() + "");
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if (edit.getText().toString().length() > 0) {
			txt_save.setEnabled(true);
		} else {
			txt_save.setEnabled(false);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
